from calm.sensors.directionSensor import DirectionSensor
from system.vec2 import Vec2
from system.gameServer import gameServer

class AgentDirectionSensor(DirectionSensor):
    def __init__(self, controller, vars, numSlices = 8):
        self.allowFilled = False
        self.minDist = 0
        
        DirectionSensor.__init__(self, controller, vars, numSlices)
        
        self.server = self.agent.searchAncestors("type","calm.calmServer")
        assert self.server, "picDirectionSensor can't find calmServer"
        
    def acceptBits(self, bits):
        pos = Vec2(self.agent.getVar("pos"))
        
        minDist = None
        for agent in self.server.agents:
            if agent.getVar("path") != self.agent.getVar("path"):
                dist = (Vec2(agent.getVar("pos")) - pos).length()
                if dist > self.minDist and (not minDist or dist < minDist):
                    minDist = dist
                    self.point = agent.getVar("pos")
                
        #print self.agent.getVar("name"), "look at", self.point
        return DirectionSensor.acceptBits(self,bits)
                
            
